/**
 * 
 */
package com.fox.webapp.service.impl;

import java.util.Date;
import java.util.List;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fox.webapp.domain.Article;
import com.fox.webapp.persistent.ArticleRepository;
import com.fox.webapp.service.IArticleService;

/**
 * @author yockii
 *
 */
@Service
public class ArticleService implements IArticleService {

	@Autowired
	private ArticleRepository articleRepository;
	
	@Override
	public Article getArticle(String articleId) {
		if(!ObjectId.isValid(articleId)) return null;
		return articleRepository.findOne(new ObjectId(articleId));
	}

	@Override
	public Article addArticle(Article article) {
		article.setTime(new Date());
		return articleRepository.save(article);
	}

	@Override
	public Article updateArticle(Article article) {
		//TODO 看看是否能够成功更新
		return articleRepository.save(article);
	}

	@Override
	public void deleteArticle(String id) {
		articleRepository.delete(new ObjectId(id));
	}

	@Override
	public Article getArticleByEnglishName(String englishName) {
		if(englishName == null) return null;
//		return articleRepository.findByTitle(title);
		return articleRepository.findByEnglishName(englishName);
	}

	@Override
	public List<Article> getAllArticle() {
		return (List<Article>) articleRepository.findAll();
	}

}
